<template>
  <div class="app-container">

    <el-card>
    <h1 align="center">施工队图片管理</h1>
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="图片描述" prop="infoDesc">
        <el-input
          v-model="queryParams.infoDesc"
          placeholder="请输入图片描述"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图片名称" prop="conUrl">
        <el-input
          v-model="queryParams.conUrl"
          placeholder="请输入图片名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否主图" prop="isMaster">
        <el-select v-model="queryParams.isMaster" placeholder="请选择图片是否主图" clearable size="small">
          <el-option label="非主图" value="0" />
          <el-option label="主图" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否有效" prop="conStatus">
        <el-select v-model="queryParams.conStatus" placeholder="请选择图片是否有效" clearable size="small">
          <el-option label="无效" value="0" />
          <el-option label="有效" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['construction:picInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['construction:picInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['construction:picInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['construction:picInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="picInfoList"  @row-dblclick="dbClickPic" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="施工队图片ID" align="center" prop="constructionPicId" />
      <el-table-column label="人员姓名" align="center" prop="constructionInfo.constructionName" />
      <el-table-column label="图片描述" align="center" prop="infoDesc" />
      <el-table-column label="图片URL" align="center" prop="conUrl" >
        <template slot-scope="scope">
          <el-image :src="scope.row.conUrl"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="是否主图" align="center" prop="isMaster">
        <template slot-scope="scope">
          <span>{{ scope.row.isMaster == 1 ? "是":"不是" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="图片排序" align="center" prop="conOrder" />
      <el-table-column label="图片是否有效" align="center">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.conStatus" active-value="1" inactive-value="0" @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="最后修改时间" align="center" prop="modifiedTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.modifiedTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleLabelAdd(scope.row)"
          >增加施工队标签</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['construction:picInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['construction:picInfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!-- 添加或修改施工队图片信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="图片描述" prop="infoDesc">
          <el-input v-model="form.infoDesc" placeholder="请输入图片描述" />
        </el-form-item>
        <el-form-item label="图片URL" prop="conUrl">
          <!--<el-input v-model="form.conUrl" placeholder="请输入图片描述" />-->

          <el-upload
            class="upload-demo"
            :http-request="upload"
            action="aaa"
            accept=".jpg,.png"
            :limit="1"
            :file-list="fileList"
            :on-remove="handleRemove"
            :before-remove="beforeRemove"
            :on-error="handleError"
            ref="upload">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">仅支持jpg,png格式,以最新上传为准</div>
          </el-upload>


        </el-form-item>
        <el-form-item label="是否主图" prop="isMaster">
          <el-switch v-model="form.isMaster" active-value="1" inactive-value="0"></el-switch>
        </el-form-item>
        <el-form-item label="图片排序" prop="conOrder">
          <el-input v-model="form.conOrder" placeholder="请输入图片排序" />
        </el-form-item>
        <el-form-item label="图片是否有效" prop="conStatus">
          <el-switch v-model="form.conStatus" active-value="1" inactive-value="0"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

      <el-divider></el-divider>
      <get-video :constructionId="this.constructionId"></get-video>

    </el-card>

    <div :style="uuu">

    <el-card>
      <el-row :gutter="10" class="mb8">

        <el-col :span="1.5">
          <el-button
            type="success"
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleclUpdate"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleclDeletecl"
          >删除</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="labelList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="标签ID" align="center" prop="labelId" />
        <!--<el-table-column label="施工队ID" align="center" prop="constructionId" />-->
        <el-table-column label="标签名称" align="center" prop="labelName" />
        <!--<el-table-column label="商品图片表ID" align="center" prop="constructionPicId" />-->
        <el-table-column label="最后修改时间" align="center" prop="modifiedTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.modifiedTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="价格" align="center" prop="labelPrice" />
        <el-table-column label="上架/下架" align="center" prop="labelStatus" >
          <template slot-scope="scope">
            <span>{{ scope.row.labelStatus == 1 ? "下架":"上架" }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleclUpdate(scope.row)"

            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleclDeletecl(scope.row)"

            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--<pagination-->
        <!--v-show="total>0"-->
        <!--:total="totalcl"-->
        <!--:page.sync="queryParamscl.pageNum"-->
        <!--:limit.sync="queryParamscl.pageSize"-->
        <!--@pagination="getListCl"-->
      <!--/>-->

      <!-- 添加或修改施工队标签对话框 -->
      <el-dialog :title="titlecl" :visible.sync="opencl" width="500px" append-to-body>
        <el-form ref="formcl" :model="formcl" :rules="rulescl" label-width="80px">

          <el-form-item label="标签名称" prop="labelName">
            <el-input v-model="formcl.labelName" placeholder="请输入标签名称" />
          </el-form-item>

          <el-form-item label="价格" prop="labelPrice">
            <el-input v-model="formcl.labelPrice" placeholder="请输入价格" />
          </el-form-item>
          <el-form-item label="上架/下架">
            <el-radio-group v-model="formcl.labelStatus">
              <el-radio label="0">上架</el-radio>
              <el-radio label="1">下架</el-radio>
            </el-radio-group>
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitFormcl">确 定</el-button>
          <el-button @click="cancelcl">取 消</el-button>
        </div>
      </el-dialog>
    </el-card>
    </div>
  </div>


</template>

<script>
  import { listPicInfo, getPicInfo, delPicInfo, addPicInfo, updatePicInfo, exportPicInfo,uploadImage } from "@/api/construction/picInfo";
  import { listLabel, getLabel, delLabel, addLabel, updateLabel, exportLabel } from "@/api/construction/conlabel";

  export default {
    name: "PicInfo",
    data() {
      return {
        uuu:'display:none',
        labelList:[],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        totalcl: 0,
        // 施工队图片信息表格数据
        picInfoList: [],
        // 图片数据
        fileList: [],
        // 弹出层标题
        title: "",
        titlecl: "",
        // 是否显示弹出层
        open: false,
        opencl: false,
        // 施工队ID
        constructionId: null,
        // 查询参数
        queryParamscl: {
          pageNum: 1,
          pageSize: 10,
          constructionId: null,
          labelName: null,
          constructionPicId: null,
          modifiedTime: null,
          labelPrice: null,
          labelStatus: null,
        },
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          constructionInfoId: null,
          infoDesc: null,
          conUrl: null,
          isMaster: null,
          conOrder: null,
          conStatus: null,
          modifiedTime: null,
        },
        // 表单参数
        form: {},
        formcl: {
          constructionPicIdNum:0,
          constructionIdNum:0,
        },
        // 表单校验
        rules: {
          conUrl: [
            { required: true, message: "图片URL不能为空", trigger: "blur" }
          ],
          isMaster: [
            { required: true, message: "是否主图：0.非主图1.主图不能为空", trigger: "blur" }
          ],
          conOrder: [
            { required: true, message: "图片排序不能为空", trigger: "blur" }
          ],
          conStatus: [
            { required: true, message: "图片是否有效：0无效 1有效不能为空", trigger: "blur" }
          ],
        },

        rulescl: {
          constructionId: [
            { required: true, message: "施工队ID不能为空", trigger: "blur" }
          ],
          labelName: [
            { required: true, message: "标签名称不能为空", trigger: "blur" }
          ],
          constructionPicId: [
            { required: true, message: "商品图片表ID不能为空", trigger: "blur" }
          ],
          modifiedTime: [
            { required: true, message: "最后修改时间不能为空", trigger: "blur" }
          ],
        }
      };
    },
    components:{
      getVideo:()=> import('@/views/construction/video/index.vue')
    },
    created() {
      let constructionId = parseInt(this.$route.query.constructionId);
      this.constructionId =  constructionId;
      this.getList(constructionId);

    },
    methods: {

      dbClickPic(row){
        var constructionPicId = row.constructionPicId;
        console.log("***constructionPicId*******"+constructionPicId)
        listLabel(constructionPicId).then(res=>{
          this.labelList = res;

      })
        this.uuu ='display:block';
      },


      handleLabelAdd(row){
        this.resetcl();
        this.opencl = true;
        this.titlecl = "添加施工队标签";
        this.formcl.constructionPicIdNum = row.constructionPicId;
        this.formcl.constructionIdNum = parseInt(this.$route.query.constructionId);
      },

      handleclUpdate(row){
        this.resetcl();
        const labelId = row.labelId || this.ids
        getLabel(labelId).then(response => {
          this.formcl = response.data;
          this.opencl = true;
          this.titlecl = "修改施工队标签";
        });
      },

      submitFormcl(){
        this.$refs["formcl"].validate(valid => {
          if (valid) {
            if (this.formcl.labelId != null) {
              updateLabel(this.formcl).then(response => {
                if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.opencl = false;
                // this.getListcl();
              }
            });
            } else {
              addLabel(this.formcl).then(response => {
                if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.opencl = false;
                // this.getListcl();
              }
            });
            }
            this.uuu='display:none'
          }
        })
      },

      handleclDeletecl(row){
        const labelIds = row.labelId || this.ids;
        this.$confirm('是否确认删除施工队标签编号为"' + labelIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delLabel(labelIds);
        }).then(() => {
          // this.getList();
        this.msgSuccess("删除成功");

      }).catch(function() {});
        this.uuu='display:none'
      },
      // 表单重置
      resetcl() {
        this.formcl = {
          labelId: null,
          constructionId: null,
          labelName: null,
          constructionPicId: null,
          modifiedTime: null,
          labelPrice: null,
          labelStatus: "0",
          delFlag: null
        };
        this.resetForm("formcl");
      },
      cancelcl(){
        this.opencl = false;
        this.resetcl();
      },
      /** 查询施工队图片信息列表 */
      getList(teamId) {
        this.loading = true;
        this.queryParams.constructionInfoId = teamId;
        listPicInfo(this.queryParams).then(response => {
          this.picInfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          constructionPicId: null,
          constructionInfoId:null,
          infoDesc: null,
          conUrl: null,
          isMaster: null,
          conOrder: null,
          fileList :null,
          conStatus: 0,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList(this.constructionId);
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.constructionPicId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },

      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加施工队图片信息";
      },

      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const constructionPicId = row.constructionPicId || this.ids
        getPicInfo(constructionPicId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改施工队图片信息";
        });
      },

      // 删除上传图片
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      // 添加上传图片
      handlePreview(file) {
        console.log(file);
      },

      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.constructionPicId != null) {
              updatePicInfo(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList(this.constructionId);
                }
              });
            } else {
              this.form.constructionInfoId = this.constructionId,
              addPicInfo(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList(this.constructionId);
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const constructionPicIds = row.constructionPicId || this.ids;
        this.$confirm('是否确认删除施工队图片信息编号为"' + constructionPicIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPicInfo(constructionPicIds);
        }).then(() => {
          this.getList(this.constructionId);
          this.msgSuccess("删除成功");
        }).catch(function() {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有施工队图片信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportPicInfo(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
      },

      //文件上传
      upload(param){
        let file = param.file;
        let fileType = file.type;
        let isLt2M = file.size / 1024 / 1024 < 500;
        if (!isLt2M) {
          this.$message.error("上传大小大小必须小于500M");
          this.fileList=[];
          return;
        }
        let form = new FormData();
        form.append("file", file);
        uploadImage(form).then((res)=>{
          if(res.code==200){
          this.form.conUrl = "http://51jhf.com/profile/xiazai/"+res.filePath;
        }
        console.log(res);
      })
      },
      //移除文件之前
      beforeRemove(file, fileList) {
        return this.$confirm(`确定移除 ${file.name}？`);
      },
      //移除
      handleRemove(file, fileList){
        this.form.conUrl = '';
        this.fileList=[];
      },
      //上传异常
      handleError(err, file, fileList){
        this.$message.error("上传失败");
      },

    }
  };
</script>
